;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Logistics.InfoEntry", [])
        .controller("InfoEntryCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$timeout", "$filter", "EventBus", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $timeout, $filter, EventBus) {
            $scope.activeFlag = [true, false, false];
            $scope.markInfo = {
                allChecked: false
            };
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };
            $scope.submitted = false;
            $scope.toolbarConfig = {
                showAdvanceSearch: false,
                showColConfig: false
            };
            $scope.condition = {};
            $scope.dataList = [];
            $scope.detailInfo = {};
            $scope.tableConfig = {
                fixedWidth: 100,
                scrollWidth: 1100,
                tableCol: [
                    {
                        type: "",
                        name: "货运单号",
                        dataField: "sendOrder",
                        width: 160,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "客户代码",
                        dataField: "deliveryOrgId",
                        width: 140,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "客户名称",
                        dataField: "deliveryOrgName",
                        width: 180,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "物流单号",
                        dataField: "logisticsOrder",
                        width: 150,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "发货日期",
                        dataField: "deliveryDateShow",
                        width: 140,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "申请日期",
                        dataField: "createDateShow",
                        width: 140,
                        isShow: true,
                        isFixed: false,
                        toSort: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "到货日期",
                        dataField: "arrivalDateShow",
                        width: 120,
                        isShow: true,
                        isFixed: false,
                        toSort: true,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "是否到货",
                        dataField: "isArrivalShow",
                        width: 90,
                        isShow: true,
                        isFixed: false,
                        sortType: ""
                    },
                    {
                        type: "",
                        name: "物流更新状态",
                        dataField: "recordFlagShow",
                        width: 140,
                        isShow: true,
                        isFixed: false
                    },
                    {
                        type: "operate",
                        name: "操作",
                        dataField: "",
                        width: 100,
                        isShow: true,
                        isFixed: true,
                        toSort: false,
                        sortType: ""
                    }
                ]
            };
            $scope.detailMap = [
                {
                    name: '送货单号',
                    field: 'sendOrder',
                    filter: "",
                    filterParam: ""
                },
                {
                    name: '客户名称',
                    field: 'deliveryOrgName',
                    filter: "",
                    filterParam: ""
                },
                {
                    name: '申请日期',
                    field: 'createDateShow',
                    filter: "YbDate",
                    filterParam: "yy-MM-dd"
                },
                {
                    name: '联系人',
                    field: 'contactUser',
                    filter: "",
                    filterParam: ""
                },
                {
                    name: '联系电话',
                    field: 'contactTel',
                    filter: "",
                    filterParam: ""
                },
                {
                    name: '收货地址',
                    field: 'address',
                    filter: "",
                    filterParam: ""
                },
                {
                    name: '发货日期',
                    field: 'sendTimeShow',
                    filter: "",
                    filterParam: ""
                },
                {
                    name: '预计到货日期',
                    field: 'deliveryDateShow',
                    filter: "",
                    filterParam: ""
                },
                {
                    name: '承运商',
                    field: 'carrierOrgName',
                    filter: "",
                    filterParam: ""
                },
            ];
            $scope.logisticsNew = {
                address: ""
            };
            $scope.setFixedWidth = function () {
                $scope.tbStyle = {scrollLeft: {left: $scope.tableConfig.fixedWidth + "px"}};
            };
            var dateFrame = "";

            $scope.changeTab = function (index) {
                $scope.activeFlag = [false, false, false];
                $scope.activeFlag[index] = true;
                $scope.submitted = false;
                if (index == 0) {
                    initData();
                }
            };

            function initData() {
                $scope.detailInfo = {};
            }

            function getDataList(curPage) {
                var service = "device";
                var reqUrl = "/deliveryReceiptLogistics/list";
                var param = {};
                param.pageSize = $scope.pageInfo.pageSize;
                if (!!curPage) {
                    $scope.pageInfo.curPage = curPage;
                }
                param.pageNum = $scope.pageInfo.curPage;
                param.filters = angular.copy($scope.condition);
                param.filters.isDelivery = 1; // 不展示未发货的单据
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(resp.value.list, function (item) {
                        item.isChecked = false;
                        item.deliveryDateShow = $filter("YbDate")(item.deliveryDate, "yyyy-MM-dd HH:mm:ss");
                        item.createDateShow = $filter("YbDate")(item.createDate, "yyyy-MM-dd HH:mm:ss");
                        item.arrivalDateShow = $filter("YbDate")(item.arrivalDate, "yyyy-MM-dd");
                        item.isArrivalShow = $filter("TrueOrFalseOrNull")(item.isArrival);
                        item.recordFlagShow = $filter("TrueOrFalseOrNull")(item.recordFlag);
                    });
                    $scope.pageInfo.totalRows = resp.value.total;
                    $scope.pageInfo.totalPages = resp.value.pages;
                    $scope.dataList = resp.value.list;
                });
            }

            $scope.toSort = function (config, index) {
                ybWidget.ToSort(config, index);
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.selectRow = function (index) {
                $scope.markInfo.cueRow = index
            };

            $scope.search = function (curpage) {
                getDataList(curpage);
            };

            $scope.reset = function () {
                $scope.condition = {};
            };
            
            function getDataDetail(sendOrder) {
                var service = "device";
                var reqUrl = "/deliveryReceiptLogistics/item4Logistics";
                var param = {};
                param.id = sendOrder;
                ybHttp.Get(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    $scope.detailInfo = resp.value;
                    $scope.detailInfo.headerInfo.createDateShow = $filter("YbDate")($scope.detailInfo.headerInfo.createDate, "yyyy-MM-dd hh:mm:ss");
                    $scope.detailInfo.headerInfo.deliveryDateShow = $filter("YbDate")($scope.detailInfo.headerInfo.deliveryDate, "yyyy-MM-dd hh:mm:ss");//预计收货时间
                    $scope.detailInfo.headerInfo.sendTimeShow = $filter("YbDate")($scope.detailInfo.headerInfo.sendTime, "yyyy-MM-dd hh:mm:ss");//发货日期

                    var lengthNum = $scope.detailInfo.logisticsVOList.length;
                    if(lengthNum > 0){
                        dateFrame = $filter("YbDate")($scope.detailInfo.logisticsVOList[0].createDate);
                        $scope.detailInfo.logisticsVOList[0].showDate = true;
                    }
                    for (var i = 1, len = lengthNum; i < len; i++) {
                        $scope.detailInfo.logisticsVOList[i].showDate = true;
                        if ($filter("YbDate")($scope.detailInfo.logisticsVOList[i].createDate) === dateFrame) {
                            $scope.detailInfo.logisticsVOList[i].showDate = false;
                        } else {
                            dateFrame = $filter("YbDate")($scope.detailInfo.logisticsVOList[i].createDate);
                        }
                    }

                    EventBus.Publish("setTimeShow", $scope.detailInfo.logisticsVOList);
                });
            }

            $scope.entryInfo = function (item) {
                $scope.currentId = item.id; // 运单主键
                getDataDetail(item.sendOrder);
                $scope.changeTab(1);
            };

            $scope.entryLogistics = function () {
                var item = {
                    billId: $scope.detailInfo.headerInfo.id,
                    billNo: $scope.detailInfo.headerInfo.sendOrder,
                    createDate: $scope.logisticsNew.createDate,
                    address: $scope.logisticsNew.address,
                    showDate: true
                };
                var service = "device";
                var reqUrl = "/deliveryReceiptLogistics/add";
                var param = angular.copy(item);
                ybHttp.Post(service, reqUrl, param).then(function (resp) {
                    if (resp.code !== 0) {
                        ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                        return;
                    }
                    var lengthNum = $scope.detailInfo.logisticsVOList.length;
                    if (lengthNum > 0) {
                        var lastDataDate = new Date($scope.detailInfo.logisticsVOList[lengthNum - 1].createDate).getTime();
                        if ($filter("YbDate")(item.createDate) === $filter("YbDate")(lastDataDate)) {
                            item.showDate = false;
                        }
                    }
                    
                    $scope.detailInfo.logisticsVOList.push(item);
                    EventBus.Publish("entryTimeFrame", $scope.logisticsNew);
                });
            };

            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            $scope.receiveGoods = function () {
                ybWidget.ShowDatePicker({name:'到货日期'}).then(function(data) {
                    var service = "device";
                    var reqUrl = "/sendBill/updateArrivalDate";
                    var param = {};
                    param.id = $scope.currentId;
                    param.arrivalDate  = $filter("YbDate")(data, "yyyy-MM-dd");;
                    ybHttp.Get(service, reqUrl, param).then(function (resp) {
                        if (resp.code !== 0) {
                            ybWidget.ShowMessage(resp, MESG_TYPE.Error);
                            return;
                        }
                        getDataList(1)
                        $scope.changeTab(0);
                    });
                })
            };

            (function () {
                getDataList();
                $scope.setFixedWidth();
            })();
        }])
    ;
})(window, window.angular);
